package com.beep.erp.common.widget.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.TextView;


import com.beep.erp.R;
import com.beep.erp.common.bean.BizStorageLocationsListBean;
import com.beep.erp.common.bean.PurchaseListBean;
import com.beep.erp.common.utils.ToolsUtils;
import com.beep.erp.common.widget.wheel.widget.OnWheelScrollListener;
import com.beep.erp.common.widget.wheel.widget.WheelView;
import com.beep.erp.common.widget.wheel.widget.adapters.AbstractWheelTextAdapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by jolly on 2018/9/7.
 */
public class SelectOneTagPopupWindow extends PopupWindow {
    public ScrollingSelectionItemLisener listener;
    private List<BizStorageLocationsListBean.BizStorageLocationsBean> list;
    private WheelView wheelView;
    private WheelView viewMonth;
    private View contentView;
    private String mTitle = "";
    private String mUnit = "";//单位
    //    private int yearIndex;
    private int currentIndex;//默认选中的下标
    private Context context;

    private static final int NO_ARRAYID = -10000;//没有资源ID


    /**
     * @param context
     * @param view
     * @param title           提示title
     * @param list            数据源
     * @param isShowUnLimited 是否展示不限
     */
    public SelectOneTagPopupWindow(final Context context, View view, String title,
                                   final List<BizStorageLocationsListBean.BizStorageLocationsBean> list, boolean isShowUnLimited) {
        this.list = list;
        initViewAndData(context, view, title, null, isShowUnLimited);
    }

    /**
     * @param context
     * @param view
     * @param title           提示title
     * @param unit            参数单位
     * @param list            数据源
     * @param isShowUnLimited 是否展示不限
     */
    public SelectOneTagPopupWindow(final Context context, View view, String title, String unit,
                                   final List<BizStorageLocationsListBean.BizStorageLocationsBean> list, boolean isShowUnLimited) {
        this.list = list;
        initViewAndData(context, view, title, unit, isShowUnLimited);
    }


    private void initViewAndData(final Context context, View view, String title, String unit, boolean isShowUnLimited) {
        mTitle = title;
        mUnit = unit;
        this.context = context;
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        contentView = inflater.inflate(R.layout.dialog_wheelview_one, null);
        this.setContentView(contentView);
        // 设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        // 设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
        // 设置SelectPicPopupWindow弹出窗体动画效果
        this.setAnimationStyle(R.style.AnimBottom);
        // 实例化一个ColorDrawable颜色为半透明

        ColorDrawable dw = new ColorDrawable(0x00000000);
//        dw.setAlpha(6);
        // 设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
        // mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
        this.showAtLocation(view,
                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
        /* 设置触摸外面时消失 */
        this.setOutsideTouchable(true);
        this.setTouchable(true);
        /* 设置点击menu以外其他地方以及返回键退出 */
//        this.setFocusable(false);
        this.backgroundAlpha((Activity) context, 0.8f);
        this.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha((Activity) context, 1f);
            }
        });
        this.update();

        // 设置监听
        wheelView = (WheelView) contentView.findViewById(R.id.wheelview_one_one);
        wheelView.setVisibleItems(24); // Number of items

        wheelView.setWheelBackground(R.drawable.wheel_bgbottom_holo);
        wheelView.setWheelForeground(R.drawable.wheel_valbottom_holo);
        wheelView.setShadowColor(0xFFffffff, 0x88ffffff, 0x00ffffff);


        wheelView.setViewAdapter(new MyAdapter(context, list));
        //hour.setViewAdapter(new HourAdapter(context));
//        view.setCurrentItem(list.size()/2);
        wheelView.setCurrentItem(0);
        wheelView.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {
            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
                initMonth();
            }
        });
//        LinearLayout ll_wheelview = (LinearLayout) contentView.findViewById(R.id.ll_wheelview);
//        ll_wheelview.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                dismiss();
//            }
//        });
        TextView tv_title = (TextView) contentView.findViewById(R.id.tv_title);
        tv_title.setText(mTitle);
        TextView tv_unit = (TextView) contentView.findViewById(R.id.tv_unit);//单位
        if (!ToolsUtils.isEmpty(unit)) {
            tv_unit.setVisibility(View.VISIBLE);
            tv_unit.setText(unit);
        } else {
            tv_unit.setVisibility(View.GONE);
        }
        Button dialog_bottom_ok = (Button) contentView.findViewById(R.id.bt_wheelview_one_ok);
        dialog_bottom_ok.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    if (!ToolsUtils.isEmptyList(list)) {

                        if (!ToolsUtils.isEmptyList(list.get(wheelView.getCurrentItem()).getBizStorageLocationVoList())) {
                            listener.onclick(list.get(wheelView.getCurrentItem()).getBizStorageLocationVoList().get(viewMonth.getCurrentItem()).getId(),
                                    list.get(wheelView.getCurrentItem()).getBizStorageLocationVoList().get(viewMonth.getCurrentItem()).getName());
                        } else {
                            listener.onclick(list.get(wheelView.getCurrentItem()).getId(), list.get(wheelView.getCurrentItem()).getName());
                        }
                    }
                }
                dismiss();
            }
        });
        Button button = (Button) contentView.findViewById(R.id.bt_wheelview_one_cancle);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        TextView tv_unlimited = (TextView) contentView.findViewById(R.id.tv_unlimited);
        if (isShowUnLimited) {
            tv_unlimited.setVisibility(View.VISIBLE);
        } else {
            tv_unlimited.setVisibility(View.GONE);
        }
        tv_unlimited.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onclick("", "不限");
                }
                dismiss();
            }
        });

        initMonth();
    }

    public void initMonth() {
        /*if (ToolsUtils.isEmptyList(list.get(wheelView.getCurrentItem()).getBizStorageLocationVoList()))
            return;*/
        viewMonth = (WheelView) contentView.findViewById(R.id.hour);
        viewMonth.setVisibleItems(24); // Number of items
        viewMonth.setWheelBackground(R.drawable.wheel_bgbottom_holo);
        viewMonth.setWheelForeground(R.drawable.wheel_valbottom_holo);
        viewMonth.setCurrentItem(0);
        viewMonth.setShadowColor(0xFFFFFFFF, 0x88FFFFFF, 0x00FFFFFF);
        viewMonth.setViewAdapter(new MonthAdapter(this.context, list.get(wheelView.getCurrentItem()).getBizStorageLocationVoList()));
        viewMonth.addScrollingListener(new OnWheelScrollListener() {
            @Override
            public void onScrollingStarted(WheelView wheel) {

            }

            @Override
            public void onScrollingFinished(WheelView wheel) {
            }
        });

    }

    //月适配器
    public class MonthAdapter extends AbstractWheelTextAdapter {

        List<BizStorageLocationsListBean.BizStorageLocationsBean.BizStorageLocationVoListBean> list = new ArrayList<>();

        protected MonthAdapter(Context context, List<BizStorageLocationsListBean.BizStorageLocationsBean.BizStorageLocationVoListBean> list) {
            super(context, R.layout.times_item_holo, AbstractWheelTextAdapter.NO_RESOURCE);
            setItemTextResource(R.id.time);
            this.list = list;
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
//            TextView time = (TextView) view.findViewById(R.id.time);
//            if(viewMonth.getCurrentItem() == index){
//                time.setTextSize(18);
//            }else {
//                time.setTextSize(14);
//            }
            return view;
        }

        public int getItemsCount() {
            return list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index).getName();
        }
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public static void backgroundAlpha(Activity context, float bgAlpha) {

        // WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha; // 0.0-1.0
        context.getWindow().setAttributes(lp);
    }

    public void setOnItemClcik(ScrollingSelectionItemLisener listener) {
        this.listener = listener;
    }


    public class MyAdapter extends AbstractWheelTextAdapter {
        List<BizStorageLocationsListBean.BizStorageLocationsBean> list = new ArrayList<>();

        protected MyAdapter(Context context, List<BizStorageLocationsListBean.BizStorageLocationsBean> list) {
            super(context, R.layout.times_item_holo, AbstractWheelTextAdapter.NO_RESOURCE);
            setItemTextResource(R.id.time);
            this.list = list;
        }

        @Override
        public View getItem(int index, View cachedView, ViewGroup parent) {
            View view = super.getItem(index, cachedView, parent);
            return view;
        }

        public int getItemsCount() {
            return list == null ? 0 : list.size();
        }

        @Override
        protected CharSequence getItemText(int index) {
            return list.get(index).getName();
        }
    }
}
